﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class admin_edcontest : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("select * from Contest where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;
        SqlParameter paramUserName = new SqlParameter("@id", SqlDbType.BigInt);
        paramUserName.Value = Request.Params["id"];
        mySqlSelect.Parameters.Add(paramUserName);
        try
        {
            SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                lg.SelectedValue = rd.GetSqlString(1).ToString();
                me.Text = rd.GetSqlString(2).ToString();
                title.Text = rd.GetSqlString(3).ToString();
                des.Value = rd.GetSqlString(4).ToString();
                nd.Value = rd.GetSqlString(5).ToString();
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("update contest set lang = @lang, menu = @menu, title = @tit, description=@des, content=@cont where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter idt = new SqlParameter("@id", SqlDbType.BigInt);
        idt.Value = Request.QueryString["id"];

        SqlParameter langt = new SqlParameter("@lang", SqlDbType.NVarChar, 2);
        langt.Value = lg.SelectedValue;

        SqlParameter menu = new SqlParameter("@menu", SqlDbType.NVarChar, 50);
        menu.Value = me.Text;

        SqlParameter tit = new SqlParameter("@tit", SqlDbType.NVarChar, 150);
        tit.Value = title.Text;

        SqlParameter desc = new SqlParameter("@des", SqlDbType.NVarChar, 500);
        desc.Value = des.Value;

        SqlParameter cont = new SqlParameter("@cont", SqlDbType.NVarChar);
        cont.Value = nd.Value;

        mySqlSelect.Parameters.Add(idt);
        mySqlSelect.Parameters.Add(langt);
        mySqlSelect.Parameters.Add(menu);
        mySqlSelect.Parameters.Add(tit);
        mySqlSelect.Parameters.Add(desc);
        mySqlSelect.Parameters.Add(cont);
        mySqlSelect.ExecuteNonQuery();
        cn.Close();
        lb.Visible = true;
        lb.Text = "Bạn đã cập nhật thành công";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?page=qlcontest");
    }
}